// 维修类型icon
import dianli1 from 'imgs/电力.png'
import dianli2 from 'imgs/电力@2x.png'
import dianli3 from 'imgs/电力@3x.png'
import kongtiao1 from 'imgs/空调.png'
import kongtiao2 from 'imgs/空调@2x.png'
import kongtiao3 from 'imgs/空调@3x.png'
import gongshui1 from 'imgs/水.png'
import gongshui2 from 'imgs/水@2x.png'
import gongshui3 from 'imgs/水@3x.png'
import dianti1 from 'imgs/电梯.png'
import dianti2 from 'imgs/电梯@2x.png'
import dianti3 from 'imgs/电梯@3x.png'
import gaoya1 from 'imgs/高压.png'
import gaoya2 from 'imgs/高压@2x.png'
import gaoya3 from 'imgs/高压@3x.png'
import paishui1 from 'imgs/排水.png'
import paishui2 from 'imgs/排水@2x.png'
import paishui3 from 'imgs/排水@3x.png'
import jiaju1 from 'imgs/家具.png'
import jiaju2 from 'imgs/家具@2x.png'
import jiaju3 from 'imgs/家具@3x.png'
import dianhua1 from 'imgs/电话.png'
import dianhua2 from 'imgs/电话@2x.png'
import dianhua3 from 'imgs/电话@3x.png'
import wangluo1 from 'imgs/网络.png'
import wangluo2 from 'imgs/网络@2x.png'
import wangluo3 from 'imgs/网络@3x.png'
import yingyin1 from 'imgs/影音.png'
import yingyin2 from 'imgs/影音@2x.png'
import yingyin3 from 'imgs/影音@3x.png'
import xiaofang1 from 'imgs/消防.png'
import xiaofang2 from 'imgs/消防@2x.png'
import xiaofang3 from 'imgs/消防@3x.png'
import jinji1 from 'imgs/紧急.png'
import jinji2 from 'imgs/紧急@2x.png'
import jinji3 from 'imgs/紧急@3x.png'

// 状态图片
import yuyue1 from 'imgs/预约中.png'
import yuyue2 from 'imgs/预约中@2x.png'
import yuyue3 from 'imgs/预约中@3x.png'
import shangmen1 from 'imgs/等待上门.png'
import shangmen2 from 'imgs/等待上门@2x.png'
import shangmen3 from 'imgs/等待上门@3x.png'
import chexiao1 from 'imgs/已撤销.png'
import chexiao2 from 'imgs/已撤销@2x.png'
import chexiao3 from 'imgs/已撤销@3x.png'
import shenpi1 from 'imgs/审批中.png'
import shenpi2 from 'imgs/审批中@2x.png'
import shenpi3 from 'imgs/审批中@3x.png'
import weixiu1 from 'imgs/维修中.png'
import weixiu2 from 'imgs/维修中@2x.png'
import weixiu3 from 'imgs/维修中@3x.png'
import wancheng1 from 'imgs/维修完成.png'
import wancheng2 from 'imgs/维修完成@2x.png'
import wancheng3 from 'imgs/维修完成@3x.png'

// 默认头像
import defautx from 'imgs/默认头像.png'

import store from '../store'
import { AxiosInst } from './axios'
import Api from './api'
// import { Toast } from 'vant'
// import 'vant/lib/toast/index.css'

export default {
  appointOvertime: 1800, // 报修后应该在1800秒内预约时间
  pageSize: 10,
  lastPages: {
    '/opinion': '/orderdetail',
    '/mreport': '/morderdetail',
    '/decision': '/morderdetail',
    '/orderdetail': '/orders',
    // '/morderdetail': '/morders',
    '/patroldetail': '/patrollist',
    '/patrolinfo': '/patrollist',
    '/orders': '/me',
    '/morders': '/me',
    '/arrange': '/me',
    '/patrollist': '/me',
    '/reportinfo': '/'
    // '/me': '/',
    // '/opersucc': (localStorage.rolecode !== '4' && localStorage.roletype === '1') ? '/mindex' : '/',
    // '/operfail': (localStorage.rolecode !== '4' && localStorage.roletype === '1') ? '/mindex' : '/'
  },
  myajax (url, data, succ, fail) {
    AxiosInst.post(url, data).then((res) => {
      res = res.data
      if (res.code === 0) {
        this.errTips(1, '')
        if (succ) succ(res.obj)
      } else {
        // Toast(res.msg)
        this.errTips(0, res.msg)
        if (fail) fail()
      }
    }).catch((err) => {
      console.log(err)
      // err.response err.request err.message err.config
      // this.errTips(-1, err.message)
      if (fail) fail()
    })
  },
  errTips (type, msg) {
    if (msg) console.log('errTipsMsg:' + msg)

    if (type === 1) {
      store.commit('set_global_error_show', false)
      store.commit('set_global_error_text', '')
    } else if (type === 0) { // 错误
      store.commit('set_global_error_show', true)
      store.commit('set_global_error_text', msg)
    } else if (type === -1) {
      let showmsg = '数据异常'
      if (msg.indexOf('Network Error') >= 0) showmsg = '网络错误'
      if (msg.indexOf('status code 500') >= 0) showmsg = '数据请求发生错误'
      store.commit('set_global_error_show', true)
      store.commit('set_global_error_text', showmsg)
    }
  },
  dealnum (num) {
    num = parseInt(num, 10)
    if (num < 10) return '0' + num
    else return '' + num
  },
  getweekstr (w) {
    var str = ''
    switch (w) {
      case 0:
        str = '日'
        break
      case 1:
        str = '一'
        break
      case 2:
        str = '二'
        break
      case 3:
        str = '三'
        break
      case 4:
        str = '四'
        break
      case 5:
        str = '五'
        break
      case 6:
        str = '六'
        break
    }
    return str
  },
  gettimestr (t) {
    var temp = parseInt(t, 10)
    var days = Math.floor(temp / 86400)
    temp = temp % 86400
    var hours = Math.floor(temp / 3600)
    temp = temp % 3600
    var minutes = Math.floor(temp / 60)
    var seconds = temp % 60

    var ret = ''
    if (days > 0) ret += days + '天'
    if (hours > 0) ret += hours + '小时'
    if (minutes > 0) ret += minutes + '分'
    if (seconds > 0) ret += seconds + '秒'

    if (ret === '') ret = '0秒'

    return ret
  },
  gettimestr2 (t) {
    var temp = parseInt(t, 10)
    var days = Math.floor(temp / 86400)
    temp = temp % 86400
    var hours = Math.floor(temp / 3600)
    temp = temp % 3600
    var minutes = Math.floor(temp / 60)
    var seconds = temp % 60

    var ret = ''
    if (days > 0) ret += days + '天 '
    ret += this.dealnum(hours) + ':'
    ret += this.dealnum(minutes) + ':'
    ret += this.dealnum(seconds)

    return ret
  },
  gettimestr3 (dobj) {
    return dobj.getFullYear() + '-' + this.dealnum(dobj.getMonth() + 1) + '-' + this.dealnum(dobj.getDate()) + ' ' + this.dealnum(dobj.getHours()) + ':' + this.dealnum(dobj.getMinutes()) + ':' + this.dealnum(dobj.getSeconds())
  },
  srcsetstr (x2, x3) {
    return x2 + ' 2x, ' + x3 + ' 3x'
  },
  getCategoryStr (cid) {
    cid = parseInt(cid, 10)
    var str = ''
    var category = store.state.category_data
    for (let i = 0; i < category.length; i++) {
      const ele = category[i]
      if (ele.id === cid) {
        str = ele.name
        break
      }
    }
    return str
  },
  getCategoryImg (name) {
    switch (name) {
      case '电力':
        return {
          imgsrc: dianli1,
          imgsrcset: this.srcsetstr(dianli2, dianli3)
        }
      case '空调':
        return {
          imgsrc: kongtiao1,
          imgsrcset: this.srcsetstr(kongtiao2, kongtiao3)
        }
      case '供水':
        return {
          imgsrc: gongshui1,
          imgsrcset: this.srcsetstr(gongshui2, gongshui3)
        }
      case '电梯':
        return {
          imgsrc: dianti1,
          imgsrcset: this.srcsetstr(dianti2, dianti3)
        }
      case '高压':
        return {
          imgsrc: gaoya1,
          imgsrcset: this.srcsetstr(gaoya2, gaoya3)
        }
      case '排水':
        return {
          imgsrc: paishui1,
          imgsrcset: this.srcsetstr(paishui2, paishui3)
        }
      case '家具':
        return {
          imgsrc: jiaju1,
          imgsrcset: this.srcsetstr(jiaju2, jiaju3)
        }
      case '电话':
        return {
          imgsrc: dianhua1,
          imgsrcset: this.srcsetstr(dianhua2, dianhua3)
        }
      case '网络':
        return {
          imgsrc: wangluo1,
          imgsrcset: this.srcsetstr(wangluo2, wangluo3)
        }
      case '影音':
        return {
          imgsrc: yingyin1,
          imgsrcset: this.srcsetstr(yingyin2, yingyin3)
        }
      case '消防':
        return {
          imgsrc: xiaofang1,
          imgsrcset: this.srcsetstr(xiaofang2, xiaofang3)
        }
      case '一键呼叫':
        return {
          imgsrc: jinji1,
          imgsrcset: this.srcsetstr(jinji2, jinji3)
        }
    }
  },
  getBuildingStr (bid) {
    bid = parseInt(bid, 10)
    var str = ''
    var buliding = store.state.building_data
    for (let i = 0; i < buliding.length; i++) {
      const ele = buliding[i]
      if (ele.id === bid) {
        str = ele.name
        break
      }
    }
    return str
  },
  getStatInfo (stat) {
    let iconsrc = ''
    let iconsrcset = ''
    let text = ''
    switch (stat) {
      case '-1':
        iconsrc = chexiao1
        iconsrcset = this.srcsetstr(chexiao2, chexiao3)
        text = '已撤销'
        break
      case '0':
      case '10':
        iconsrc = yuyue1
        iconsrcset = this.srcsetstr(yuyue2, yuyue3)
        text = '预约中'
        break
      case '11':
        iconsrc = shangmen1
        iconsrcset = this.srcsetstr(shangmen2, shangmen3)
        text = '等待上门维修'
        break
      case '20':
      case '31':
        iconsrc = weixiu1
        iconsrcset = this.srcsetstr(weixiu2, weixiu3)
        text = '维修中'
        break
      case '30':
        iconsrc = shenpi1
        iconsrcset = this.srcsetstr(shenpi2, shenpi3)
        text = '审批中'
        break
      case '50':
        iconsrc = wancheng1
        iconsrcset = this.srcsetstr(wancheng2, wancheng3)
        text = '维修完成'
        break
    }

    return {iconsrc, iconsrcset, text}
  },
  getTabUrl (ty) {
    let rcode = localStorage.rolecode
    let indexurl = ''
    if (rcode !== '4' && ty === '1') {
      indexurl = '/mindex'
    } else {
      indexurl = '/'
    }

    return {
      index: indexurl,
      tab2: '/me'
    }
  },
  getFullFileUrl (furl) {
    return Api.domain + furl
  },
  getFileType (tystr) {
    if (/^(image)\/[\s\S]*/.test(tystr)) { // 图片
      return 'image'
    } else if (/^(video)\/[\s\S]*/.test(tystr)) { // 视频
      return 'video'
    }
  },
  getFileType2 (str) {
    if (/^(.+)(\.)(jpg|png|gif|jpeg|JPG|PNG|GIF|JPEG)$/.test(str)) { // 图片
      return 'image'
    } else if (/^(.+)(\.)(mp4|mov|avi|3gp|MP4|MOV|AVI|3GP)$/.test(str)) { // 视频
      return 'video'
    }
  },
  getAvatar (avatarurl) {
    if (avatarurl === '' || avatarurl === '0' || avatarurl === null) return defautx
    else return avatarurl
  },
  getOSType () {
    let u = navigator.userAgent
    if (u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/)) return 'IOS' // ios终端
    else if (u.indexOf('Android') > -1 || u.indexOf('Linux') > -1) return 'Android' // android终端或者uc浏览器
    else return ''
  },
  getRoleVal (rstr, prefix) {
    var ret = ''
    if (rstr) {
      var rstrarr = rstr.split('·')
      ret = rstrarr[rstrarr.length - 1]
      if (prefix) ret = prefix + ret
    }
    return ret
  },
  overscroll (el) {
    el.addEventListener('touchstart', function () {
      var top = el.scrollTop
      var totalScroll = el.scrollHeight
      var currentScroll = top + el.offsetHeight
      // If we're at the top or the bottom of the containers
      // scroll, push up or down one pixel.
      //
      // this prevents the scroll from "passing through" to
      // the body.
      if (top === 0) {
        el.scrollTop = 1
      } else if (currentScroll === totalScroll) {
        el.scrollTop = top - 1
      }
    })
    el.addEventListener('touchmove', function (evt) {
      // if the content is actually scrollable, i.e. the content is long enough
      // that scrolling can occur
      if (el.offsetHeight < el.scrollHeight) evt._isScroller = true
    })
  }
}
